package top.igotcha.eduservice.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import top.igotcha.eduservice.entity.EduVideo;
import top.igotcha.eduservice.service.EduVideoService;
import top.igotcha.commonutils.exception.GuliException;
import top.igotcha.commonutils.utils.R;

/**
 * <p>
 * 课程视频(小结) 前端控制器
 * </p>
 *
 * @author Gotcha
 * @since 2021-08-06
 */
@RestController
@RequestMapping("/eduservice/video")
//@CrossOrigin
public class EduVideoController {
    @Autowired
    private EduVideoService videoService;

    @PutMapping()
    public R addVideo(@RequestBody EduVideo video){
        videoService.save(video);
        return R.OK();
    }

    @DeleteMapping("{videoId}")
    public R deleteVideo(@PathVariable String videoId){
        EduVideo video = videoService.getById(videoId);
        String videoSourceId = video.getVideoSourceId();
        if(!StringUtils.isEmpty(videoSourceId)){
            R r = videoService.removeFromAliYun(videoSourceId);
            if(r.getCode()==20001){
                throw new GuliException(20001,"删除视频失败，触发熔断器");
            }
        }
        boolean flag = videoService.removeById(videoId);
        if(flag){
            return R.OK();
        } else {
            return R.ERROR();
        }
    }

    @PostMapping()
    public R updateVideo(@RequestBody EduVideo video){
        videoService.updateById(video);
        return R.OK();
    }

    @GetMapping("{videoId}")
    public R getVideo(@PathVariable String videoId){
        EduVideo video = videoService.getById(videoId);
        return R.OK().data("video",video);
    }
}

